<template>
  <Form ref="resourceInfoFormRef" :items="items" split-col="1" width="500px" style="margin-top: 20px;padding-right: 100px;"
        label-width="80" :show-reset="showReset" :rules="rules" @validateSuccess="validateSuccess">

  </Form>
</template>

<script setup>

import Form from "@/components/plugs/form/Form.vue";
import {FormItemType} from "@/components/plugs/form/DialogFormInterface.js";
import {AXIOS_NAME} from "@/constant/constant.js"
import {inject, ref, toRaw} from "vue";
import {ElMessage} from "element-plus";

const $axios = inject(AXIOS_NAME);
const resourceInfoFormRef = ref(null);
const showReset = false;

const checkQuantity = (rule, value, callback) =>{
  if(value > 0 || value == '' || value == undefined){
    callback();
    return;
  }
  return callback(new Error('数量必须大于0'));
}

let items = [];
items.push({type: FormItemType.INPUT_HIDDEN,label: 'id',name: 'id'});
items.push({type: FormItemType.INPUT_NUMBER,label: 'cpu',name: 'cpu',placeholder:'填写值大于0'});
items.push({type: FormItemType.INPUT_NUMBER,label: '内存',name: 'memory',placeholder:'填写值大于0，单位为M'});
items.push({type: FormItemType.INPUT_NUMBER,label: '磁盘空间',name: 'disk',placeholder:'填写值大于0，单位为M'});

const rules = {
  cpu: [
    { validator: checkQuantity, trigger: 'blur' }
  ],
  memory: [
    { validator: checkQuantity, trigger: 'blur' }
  ],
  disk: [
    { validator: checkQuantity, trigger: 'blur' }
  ]
};

const validateSuccess = (data) =>{
  $axios.post("/deploy/updateResourceInfo",data).then(response =>{
    const data = response.data;
    if(data.code == 1){
      ElMessage.info(data.msg);
      return;
    }
    ElMessage.error(data.msg);
  });
}

const setData = (data) =>{
  resourceInfoFormRef.value.setData(toRaw(data));
}

defineExpose({setData});

</script>

<style scoped>

</style>